import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    products: []
  },
  mutations: {
    addProduct(state, product) {
      let temp = state.products;
      let falg = temp.some(item => item.iid === product.iid);

      if (falg) {
        // 已经有该商品
        state.products = temp.map(item => {
          if (item.iid === product.iid) {
            item.num++;
          }
          item.checked = false;
          return item;
        })

      } else {
        //没有过该商品
        state.products.push(product);
      }


    },
    clearProduct(state) {
      state.products = state.products.filter(item => !item.checked);
    }
  },
  getters: {},
  actions: {},
  modules: {}
})
